/**
 * Filename: InviteCodectivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/16 18:17
 */
package com.github.postsanf.yinian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.GetContactUser;
import com.github.postsanf.yinian.utils.YNInviteSecret;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

/**
 * 生成邀请码入组
 */
public class InviteCodectivity extends BaseActivity implements View.OnClickListener {
    private ImageView btn_contact_share;
    private TextView tv_create_invite_code;
    private Tencent mTencent;
    private BaseUiListener listener;
    private String inviteKey;
    private String inviteUrl;
    private String title;
    private YNAlbum curAlbum;
    private ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_invite_choose_code);
        mTencent = Tencent.createInstance(getString(R.string.QQ_APP_ID), this.getApplicationContext());
        listener = new BaseUiListener();
        curAlbum = YNSingleton.getInstance().getCurAlbum();
        doInvateKey();
        initView();
    }

    private void doInvateKey() {
        String invite = CommonConstants.YNS_USER_ID + "=" + application.getCurID() + "," + CommonConstants.YNS_GROUP_ID + "=" + curAlbum.getGroupid();
        try {
            inviteKey = YNInviteSecret.encryptDES(invite, getString(R.string.UNKNOW_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        inviteUrl = getString(R.string.NET_ADDRESS) + inviteKey;
    }

    private void initView() {
        title = application.getCurUser().getUnickname() + " 邀请你加入[" + curAlbum.getGname() + "]";
        btn_contact_share = (ImageView) findViewById(R.id.btn_contact_share);
        tv_create_invite_code = (TextView) findViewById(R.id.tv_create_invite_code);
        iv_back = ((ImageView) findViewById(R.id.iv_back));
        iv_back.setOnClickListener(this);
        btn_contact_share.setOnClickListener(this);
        tv_create_invite_code.setText(curAlbum.getGinvite());
        tv_create_invite_code.setOnClickListener(this);
        // 获取本机联系人信息
        GetContactUser.GetContactUser(InviteCodectivity.this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btn_qq_share:
//                String qdesc  = "忆年，一款基于亲密关系的图片分享云相册，在点赞之交的泛社交下为你守护最亲密的人。";
//                ShareUtils.onInvateFromQQ(mTencent, this, inviteUrl, title, qdesc, listener);
//                Intent statintentqq = new Intent(ACTIONs.record);
//                statintentqq.putExtra("key", "qqInvite_5");
//                statintentqq.putExtra("value", 1);
//                sendBroadcast(statintentqq);
//                break;
            case R.id.btn_contact_share:
                intent2Activity(ContactInviteActivity.class);
                Intent statintent = new Intent(ACTIONs.record);
                statintent.putExtra("key", "messageInvite_5");
                statintent.putExtra("value", 1);
                sendBroadcast(statintent);
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_create_invite_code:
                ClipboardManager clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                clip.setText(tv_create_invite_code.getText().toString().trim());
                showToast("复制邀请码成功");
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Tencent.onActivityResultData(requestCode, resultCode, data, listener);
    }

    private class BaseUiListener implements IUiListener {
        protected void doComplete(JSONObject values) {
        }

        @Override
        public void onComplete(Object o) {
            showLog(o.toString());
            doComplete((JSONObject) o);
        }

        @Override
        public void onError(UiError e) {
            showLog("onError:====code:" + e.errorCode + ", msg:"
                    + e.errorMessage + ", detail:" + e.errorDetail);
        }

        @Override
        public void onCancel() {
            showLog("onCancel=====");
        }
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
